package com.hewep.demo.device.mina.core;

import java.lang.reflect.InvocationTargetException;

public class ActionInvocation {
	private Controller controller;
	private Action action;
	
	private static final Object[] NULL_ARGS = new Object[0];
	
	ActionInvocation(Action action, Controller controller) {
		this.controller = controller;
		this.action = action;
	}
	
	public void invoke() {
		try {
			action.getMethod().invoke(controller, NULL_ARGS);
		}
		catch (InvocationTargetException e) {
			Throwable cause = e.getTargetException();
			if (cause instanceof RuntimeException)
				throw (RuntimeException)cause;
			throw new RuntimeException(e);
		}
		catch (RuntimeException e) {
			throw e;
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
